﻿using Common;
using DMTroube_ZeKer.Control;
using HtmlAgilityPack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DMTroube_ZeKer.View.GiftPage;

namespace DMTroube_ZeKer.View.ToolView
{
    public partial class ControlTool : UserControl
    {
        public MainWebView _webView;
        public ControlTool(MainWebView webView)
        {
            _webView = webView;
            InitializeComponent();
            button1.Click += Button1_Click;
            _webView.LikeInfo += _webView_LikeInfo;

        }

        private void _webView_LikeInfo(object? arg1, global::Model.Msg.ResultAnalysisInfo arg2)
        {
            likeInfo.Text = arg2.UserName.TrimEnd() + ":" + arg2.Content;
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            _webView.GetDm();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            GiftPage giftPage = new GiftPage();
            giftPage.Show();
        }

        private async void button3_Click(object sender, EventArgs e)
        {
            string num = await _webView.webView2.CoreWebView2.ExecuteScriptAsync("document.getElementsByClassName('EXdEbk58 SSufpRIb')[0].children[0].children[0].children.length");
            List<GiftPage.LW> giftList = new List<GiftPage.LW>();
            for (int i = 0; i < int.Parse(num); i++)
            {
                GiftPage.LW lW = new GiftPage.LW();
                string giftSrc = (await _webView.webView2.CoreWebView2.ExecuteScriptAsync("document.getElementsByClassName('EXdEbk58 SSufpRIb')[0].children[0].children[0].children[" + i + "].getElementsByClassName('ajuAv7KP')[0].src")).Trim('\"');
                string giftPrice = (await _webView.webView2.CoreWebView2.ExecuteScriptAsync("document.getElementsByClassName('EXdEbk58 SSufpRIb')[0].children[0].children[0].children[" + i + "].getElementsByClassName('qO_N1Mn_')[0].innerText")).Trim('\"');
                string giftName = (await _webView.webView2.CoreWebView2.ExecuteScriptAsync("document.getElementsByClassName('EXdEbk58 SSufpRIb')[0].children[0].children[0].children[" + i + "].getElementsByClassName('DCwD5QXG')[0].innerText")).Trim('\"');
                lW.name = giftName;
                lW.src = giftSrc;
                lW.price = giftPrice;
                if (!Main.lwlist.Any(n => n.src == giftSrc && n.name == giftName && n.price == giftPrice))
                    giftList.Add(lW);
            }
            if (giftList.Count > 0)
            {
                Main.lwlist.AddRange(giftList);
                string absolutePath = System.IO.Path.GetFullPath("./Resources/Data/lw.json");
                JsonHelper.WriteJsonFile(absolutePath, JsonConvert.SerializeObject(Main.lwlist));
                MessageBox.Show("获取成功");
            }
            else
            {
                MessageBox.Show("未检测到新礼物信息");
            }
        }

        private void ControlTool_Load(object sender, EventArgs e)
        {

        }
    }
}
